package com.example.studentmanager.service.impl;

import com.example.studentmanager.mapper.StudentMapper;
import com.example.studentmanager.mapper.TeacherMapper;
import com.example.studentmanager.model.Student;
import com.example.studentmanager.model.Teacher;
import com.example.studentmanager.service.TeacherService;
import com.example.studentmanager.utils.BaseDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Component
@Service
public class TeacherServiceImpl implements TeacherService{


    @Autowired
    List<Teacher> teachers;

    SqlSession sqlSession= new BaseDao().getSession();


    TeacherMapper teacherMapper=sqlSession.getMapper(TeacherMapper.class);

    @Override
    public List<Teacher> searchTeach() {
     teachers=new ArrayList<Teacher>();
        TeacherMapper teacherMapper=sqlSession.getMapper(TeacherMapper.class);
        teachers=teacherMapper.selectByTeachCour();
        return teachers;
    }

    @Override
    public void addTeacher(Teacher teacher) {
        teacherMapper.insertTeacher(teacher);
        sqlSession.commit();
    }

    @Override
    public void delTeacher(Integer teaid, String name) {
        teacherMapper.deleteTeacher(teaid,name);
        sqlSession.commit();
    }

    @Override
    public List<Teacher> searchAllTeacher() {
        List<Teacher>  teachers=teacherMapper.selectAll();
        return teachers;
    }

    @Override
    public Teacher searchTeacher(Integer teaid) {
        Teacher teacher=teacherMapper.selectTeacher(teaid);
        System.out.println(teacher.getName());
        return teacher;
    }

    @Override
    public void updateTeacher(Teacher teacher) {
        teacherMapper.updateTeacher(teacher);
        sqlSession.commit();
    }

    @Override
    public List<Teacher> blurSearchTeacher(String str) {
        String str1="%"+str+"%";
        List<Teacher> teachers=teacherMapper.blurSelectTeacher(str1);
        return teachers;
    }


    @Override
    public Integer counts(String searchCol, String searchValue) {
        return teacherMapper.counts(searchCol, searchValue);
    }


    @Override
    public List<Teacher> searchAll(Integer page, Integer rows, String searchCol, String searchValue) {
        Integer start=(page-1)*rows;
        List<Teacher> teachers= null;
        try {
            teachers = teacherMapper.searchAll(start,rows,searchCol,searchValue);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(teachers!=null)
            return teachers;
        else
            throw new RuntimeException("未查询到");
    }
}
